#include "mapdisplayitem.h"
#include <QtDebug>

MapDisplayItem::MapDisplayItem(float len, QGraphicsItem *parent) :
    QGraphicsItem(parent)
{
    qDebug() << "Got map length " << len;
    mapLength = len;
    playerPosition = 0.0;
    opponentPosition = 0.0;

    setScale(1.0);
}

void MapDisplayItem::setSize(int x, int y)
{
    width = x;
    height = y;
}

void MapDisplayItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setPen(Qt::white);
    painter->setBrush(QBrush(QColor(0,0,0,127)));
    painter->drawRect(QRectF(0,0,width,height));

    painter->fillRect(QRectF(0, height / 2.0 - (width / 2.0), width, width), Qt::white);

    if(mapLength > 0.0)
    {
        painter->setBrush(Qt::red);
        painter->drawEllipse(QPointF(width / 2.0,
                                    height / 2.0 * (opponentPosition / mapLength)), 5.0, 5.0);

        painter->setBrush(Qt::green);
        painter->drawEllipse(QPointF(width / 2.0,
                                    height - (height / 2.0 * (playerPosition / mapLength))), 5.0, 5.0);
    }
}

QRectF MapDisplayItem::boundingRect() const
{
    return QRectF(10, 10, width, height);
}

void MapDisplayItem::updatePositions(float player, float opponent)
{
    playerPosition = player;
    opponentPosition = opponent;
}

void MapDisplayItem::setLength(float len)
{
    mapLength = len;
}
